package org.project.s.controller;

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;

import org.project.pojo.CityAddress;
import org.project.pojo.CityAddress0;
import org.project.service.user.CityAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/cityShopcontroller")
public class CityShopcontroller {
@Resource
private CityAddressService cityAddressService;
@RequestMapping("/selectcityall1")
public String selectcityall1(){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String path=getLoaclURL(req)+"shell//";
	List<CityAddress0> list=cityAddressService.selectcityaddress1();
	List<CityAddress> list2=new ArrayList<CityAddress>();
	if(null!=list){
		Iterator<CityAddress0> it=list.iterator();
		while(it.hasNext()){
			CityAddress cityAddress=new CityAddress();
			CityAddress0 cityAddress0=it.next();
			cityAddress.setCityName(cityAddress0.getCityName());
			cityAddress.setId(cityAddress0.getId());
			cityAddress.setShopName(cityAddress0.getShopName());
			cityAddress.setShopAddress(cityAddress0.getShopAddress());
			cityAddress.setTelephone(cityAddress0.getTelephone());
			cityAddress.setLatitude(cityAddress0.getLatitude());
			cityAddress.setLongitude(cityAddress0.getLongitude());
			cityAddress.setUrl(path+cityAddress0.getImgname());
			cityAddress.setStartTime(cityAddress0.getStartTime());
			cityAddress.setEndTime(cityAddress0.getEndTime());
			list2.add(cityAddress);
		}
	}
	  System.out.println(list2);
	 req.setAttribute("shoplist", list2);
	
	return "showcityaddress";
}


@RequestMapping("/editcityaddress1")
public String editcityaddress1(@Param("id")int id){
	System.out.println(id);
	CityAddress c0=new CityAddress();
	c0.setId(id);
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	CityAddress0 cityAddress0 = cityAddressService.editcityaddress1(c0);

	req.setAttribute("cityAddress", cityAddress0);
	
	
	return "xiugaishop";
}








@RequestMapping("/editcityaddress2")
public String editcityaddress2(CityAddress0 cityaddress0,@ RequestParam MultipartFile items_pic){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String originalFilename = items_pic.getOriginalFilename();
	
	if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
	String pic_path=getRealPath(req)+"shell//";
	//新图片名称
	String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
//	System.out.println(newFileName);
	 System.out.println(items_pic.getOriginalFilename());
	File newFile=new File(pic_path+newFileName);
	cityaddress0.setImgname(newFileName);
	
	
	try {
	items_pic.transferTo(newFile);
	
	
} catch (IllegalStateException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	 
	
	}
	int i=cityAddressService.editcityaddress2(cityaddress0);
	
	if(i>0){
	return "mainfra";
	
	}else{
		return "";
	}
}


@RequestMapping("/addcityaddress")
public String addcityaddress(CityAddress0 cityaddress0,@ RequestParam MultipartFile items_pic){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	String originalFilename = items_pic.getOriginalFilename();
	
	if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
	String pic_path=getRealPath(req)+"shell//";
	//新图片名称
	String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
	/*System.out.println(newFileName);
	 System.out.println(items_pic.getOriginalFilename());*/
	File newFile=new File(pic_path+newFileName);
	cityaddress0.setImgname(newFileName);
	
	
	try {
	items_pic.transferTo(newFile);
	
	
} catch (IllegalStateException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	 
	
	}
	int i=cityAddressService.addcityaddress(cityaddress0);
	
	if(i>0){
	return "mainfra";
	
	}else{
		return "";
	}
}


@RequestMapping("/deleteaddress")
public String deleteaddress(@Param("id")int id){
	CityAddress c0=new CityAddress();
	c0.setId(id);
	int i=cityAddressService.deleteaddress(c0);
	if(i>0){
		return "mainfra";
	}else{
		return "";
	}
	
}

 

/**
 * @方法名:获取服务器盘符路径
 * @参数:
 * @输出:
 * @备注:
 * @作者: 
 * @时间: 2017年7月5 11:42:57
 * @修改:
 */

public static String getRealPath(HttpServletRequest request) {

	return request.getSession().getServletContext().getRealPath("/");
}





public static String getLoaclURL(HttpServletRequest request) {
	
	String path_now = request.getContextPath();
	return request.getScheme() + "://" + request.getServerName() + ":"
			+ request.getServerPort() + path_now + "/";
}



}
